﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmSocketTest
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            bool ret = true;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
            try
            {
                if (ret)
                {
                    // Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                    Application.ThreadException += Application_ThreadException;
                    //处理非UI线程异常
                    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                    SocketClient.GetSocketClient.ConnectSocket(true);
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new FrmMain());
                    mutex.ReleaseMutex();
                }
                else
                {
                    MessageBox.Show(string.Format("有一个和本程序相同的应用程序已经在运行，请不要同时运行多个本程序。\n\n这个程序即将退出。"));
                    Application.Exit();//退出程序
                }
            }
            catch (Exception ex)
            {
                CommHelper.LogHelper.WriteErrorLog(ex.Message);
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            CommHelper.LogHelper.WriteErrorLog((e.ExceptionObject as Exception).Message);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            CommHelper.LogHelper.WriteErrorLog(e.Exception.Message);
        }
    }
}